/*
 * TTSEngine
 * author ydwang3
 * create at 2017/7/3 14:31
 * CopyRight IFLYTEK Co.,Ltd. All Rights Reserved.
 */
package com.iflytek.speech.sample;

import android.util.Log;

import com.iflytek.speech.ISSErrors;
import com.iflytek.speech.NativeHandle;
import com.iflytek.speech.libisstts;
import com.iflytek.speech.tts.ITtsListener;

/**
 * TTS引擎集成封装示例
 */
public class TTSEngine implements ITtsListener {

    private static final String TAG = "TTSEngine";

    private final NativeHandle NATIVE_HANDLE = new NativeHandle();

    private ITTSListener mListener;

    private int nPreTextIndex = -1;

    private boolean initialized = false;

    /**
     * 初始化TTS引擎
     * @param resDir
     * @return
     */
    public synchronized int init(String resDir) {
        Log.d(TAG, "Init");
        if (!initialized) {
            String code = "{\"cpu_info\": \"aiui_21001001180626170000013860613245\",\"key\": [\"cpu_info\"],\"path\": \"/sdcard/iflytek/lesl_demo.v2c\"}";
            int ret = libisstts.setMachineCode(code);
            if(ret != ISSErrors.ISS_SUCCESS){
                Log.e(TAG, "set machine code failed ret :" + ret);
            }

            // 初始化TTS引擎资源(mode = 0，为读文件方式)
            int lib_ret = libisstts.initRes(resDir, 0);
            if (lib_ret != ISSErrors.ISS_SUCCESS) {
                Log.e(TAG,"init() initRes failed ret = "+lib_ret);
                return lib_ret;
            }
            // 创建TTS实例
            libisstts.create(NATIVE_HANDLE, this);
            if (NATIVE_HANDLE.err_ret != ISSErrors.ISS_SUCCESS) {
                Log.e(TAG,"init() create failed ret = "+ NATIVE_HANDLE.err_ret);
                return NATIVE_HANDLE.err_ret;
            }
            initialized = true;
        }
        return ISSErrors.ISS_SUCCESS;
    }

    /**
     * 设置回调监听
     * @param listener
     */
    public void setListener(ITTSListener listener) {
         mListener = listener;
    }

    ITTSListener getListener() {
        synchronized (this) {
            return mListener;
        }
    }

    /**
     * 设置TTS引擎参数（此处通常是设置发音人接口）
     * @param nParam 参数名称
     * @param nValue  参数值
     * @return
     */
    public int setParam(int nParam, int nValue) {
        Log.d(TAG, "SetParam");
        if (NATIVE_HANDLE.native_point == 0) {
            return ISSErrors.ISS_ERROR_INVALID_CALL;
        }
        libisstts.setParam(NATIVE_HANDLE, nParam, nValue);
        return NATIVE_HANDLE.err_ret;
    }

    /**
     * 设置TTs引擎String类型参数
     * @param nParam 参数名称
     * @param nValue 参数值
     * @return
     */
    public int setParamEx(int nParam, String nValue) {
        Log.d(TAG, "SetParam");
        if (NATIVE_HANDLE.native_point == 0) {
            return ISSErrors.ISS_ERROR_INVALID_CALL;
        }
        libisstts.setParamEx(NATIVE_HANDLE, nParam, nValue);
        return NATIVE_HANDLE.err_ret;
    }

    /**
     * 开始合成音频
     * @param text 合成文本
     * @return
     */
    public int start(String text) {
        Log.d(TAG, "Start");
        stop();
        libisstts.start(NATIVE_HANDLE, text);
        Log.d(TAG, "Start end");
        return NATIVE_HANDLE.err_ret;
    }

    /**
     * 停止合成音频
     * @return
     */
    public int stop() {
        Log.d(TAG, "Stop");
        libisstts.stop(NATIVE_HANDLE);
        Log.d(TAG, "Stop end");
        return NATIVE_HANDLE.err_ret;
    }

    /**
     * 释放TTs引擎实例
     * @return
     */
    int release() {
        Log.d(TAG, "start destroy tts engine");
        //销毁实例
        libisstts.destroy(NATIVE_HANDLE);
        //释放资源加载
        libisstts.unInitRes();
        return NATIVE_HANDLE.err_ret;
    }

    int getAudioData(byte[] var1, int var2, int[] var3) {
        libisstts.getAudioData(NATIVE_HANDLE, var1, var2, var3);
        return NATIVE_HANDLE.err_ret;
    }

    public void onDataReady() {
        if (null != mListener) {
            mListener.onDataReady();
        }
    }

    @Override
    public void onTtsMsgProc(int i, int i1, String s) {
        Log.d(TAG, "onTtsMsgProc:i=" + i + ", i1=" + i1 + ", s=" + s);
        if (i == 20000) {
            //本地开始合成
            onDataReady();
        }
    }

    @Override
    public void onProgress(int nTextIndex, int nTextLen) {
        // 过滤重复的进度信息
        if (nPreTextIndex < nTextIndex) {
            Log.d(TAG, "onProgress(" + nTextIndex + ", " + nTextLen + ")");
            if (mListener != null) {
                mListener.onProgress(nTextIndex, nTextLen);
            }
        }
        nPreTextIndex = nTextIndex;
    }

    public interface ITTSListener {
        void onDataReady();

        void onProgress(int index, int len);

        void onInterrupted();
    }
}
